package com.itheima.ydd.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.PageResult;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.entity.User;
import com.itheima.ydd.entity.UserDemo;
import com.itheima.ydd.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("sys_user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;


    /**
     * 分页查询
     * @param page
     * @param limit
     * @param phone
     * @param status
     * @param login_name
     * @return
     */
    @GetMapping("/list")
        public ResultData list(int page,int limit,String phone,Integer status,String login_name){
        Page<User> pageInfo=new Page<>(page,limit);
        LambdaQueryWrapper<User> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like(phone!=null,User::getPhone,phone)
                .like(login_name!=null,User::getLogin_name,login_name);
        queryWrapper.eq(status!=null,User::getStatus,status);
        queryWrapper.orderByAsc(User::getUpdate_date);
        Page<User> page1 = userService.page(pageInfo, queryWrapper);
        List<User> records = page1.getRecords();
        long counts = userService.count();
        long pageSize=(long)limit;
        long currentPage=(long)page;
        return ResultData.ok("",PageResult.init(counts,pageSize,currentPage,records));
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @GetMapping("/del")
    public ResultData delete(@RequestParam String id){
        userService.remove(id);
        return ResultData.ok("","删除成功");
    }

    /**
     * 添加
     * @param
     * map
     * @return
     */
    @PostMapping("/add")
    public ResultData add(@RequestBody Map map) {

        User user=new User();
        user.setName ((String)map.get("name"));

        String login_name = (String) map.get("login_name");
        user.setLogin_name(login_name);
        String password = (String) map.get("password");
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        user.setPassword(password);
        user.setEmail(map.get("email").toString());
        user.setPhone(map.get("phone").toString());

        List<String> role_id = (List<String>) map.get("role_id");
        List<String> role=new ArrayList<>();
        for (String s : role_id) {
            role.add("\""+s+"\"");
        }

        user.setRole_id(role.toString());
        user.setStatus(Integer.parseInt(map.get("status").toString()));

        userService.save(user);
        return ResultData.ok("", " 添加成功");
    }

    /**
     * 数据回显
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public ResultData getById(String id){
        LambdaQueryWrapper<User> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getId,id);
        User user = userService.getOne(queryWrapper);
        String role_id = user.getRole_id();
        String[] split = role_id.replace("[", "").replace("]", "").split(",");
        List<String> list=new ArrayList<>();

        for (String s : split) {
            list.add(s.replace("\"","").trim());
        }
       // list.add(role_id);
 //       System.out.println(list);
        UserDemo userDemo=new UserDemo();
        userDemo.setId(id);
        userDemo.setName(user.getName());
        userDemo.setLogin_name(user.getLogin_name());
        userDemo.setEmail(user.getEmail());
        userDemo.setPhone(user.getPhone());
        userDemo.setRole_id(list);
        userDemo.setStatus(user.getStatus());

//        Map map=new HashMap();
//        map.put("name",user.getName());
//        map.put("login_name",user.getLogin_name());
//        map.put("email",user.getEmail());
//        map.put("phone",user.getPhone());
//        map.put("role_id",list);
//        map.put("status",user.getStatus());
//        map.put("id",id);
        return ResultData.ok("",userDemo);
    }

    /**
     * 修改
     */
    @PostMapping("update")
    public ResultData update(@RequestBody UserDemo userDemo){
        String id =  userDemo.getId();
        User user = userService.getById(id);
        if (userDemo.getRole_id()!=null){
        user.setName (userDemo.getName());

        String login_name = userDemo.getLogin_name();
        user.setLogin_name(login_name);

        user.setEmail(userDemo.getEmail());
        user.setPhone(userDemo.getPhone());
        List<String> role_id = userDemo.getRole_id();
        List<String> role=new ArrayList<>();
        for (String s : role_id) {
            role.add("\""+s+"\"");
        }
        user.setRole_id(role.toString());
        user.setStatus(userDemo.getStatus());
        }else {
        user.setStatus(userDemo.getStatus());
        }
        userService.updateById(user);
        return ResultData.ok("", " 修改成功");
    }
}
